package com.atguigu.gulimail.product.app;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimail.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimail.product.service.AttrService;
import com.atguigu.gulimail.product.service.ProductAttrValueService;
import com.atguigu.gulimail.product.vo.AttrEntityRespVO;
import com.atguigu.gulimail.product.vo.AttrEntityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品属性
 *
 * @author fangyi
 * @email 2244210628@qq.com@gmail.com
 * @date 2022-08-09 22:57:13
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    ProductAttrValueService productAttrValueService;

    //    /product/attr/base/listforspu/{spuId}
    @RequestMapping("/base/listforspu/{spuId}")
    public R baseAttrlistforspu(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueEntity> entityList = productAttrValueService.baseAttrlistforspu(spuId);

        return R.ok().put("data", entityList);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    @RequestMapping("/{type}/list/{catlogId}")
    //@RequiresPermissions("product:attr:list")
    public R Baselist(@RequestParam Map<String, Object> params,
                      @PathVariable("catlogId") Long catlogId,
                      @PathVariable("type") String type) {
        PageUtils page = attrService.queryBasePage(params, catlogId, type);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {

        AttrEntityRespVO attr = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attr);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrEntityVO attr) {

        attrService.saveAttrAndGroup(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntityVO attr) {
        attrService.updateAttr(attr);

        return R.ok();
    }


    /**
     * 更新规格参数
     * @param spuId
     * @param attr
     * @return
     */
    @RequestMapping("/update/{spuId}")
    public R updateAttrList(@PathVariable("spuId") Long spuId,
                            @RequestBody List<ProductAttrValueEntity> attr) {
        productAttrValueService.updateAttrNormList(spuId,attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
